package com.stars.distributed.schedule.demo.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.stars.distributed.schedule.starter.DistributedScheduleStarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * spring boot 数据源的配置类
 *
 * @author guoguifang
 */
@Configuration
public class DataSourceConfig {

    @Bean
    public DistributedScheduleStarter distributedScheduleStarter() {
        return new DistributedScheduleStarter();
    }

    /**
     * 创建数据源
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    public DataSource dataSource(Properties dataSourceProperties) throws Exception {
        for (Object key : dataSourceProperties.keySet()) {
            Object value = dataSourceProperties.get(key);
            if (value != null) {
                dataSourceProperties.put(key, value.toString());
            }
        }
        return DruidDataSourceFactory.createDataSource(dataSourceProperties);
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource")
    public Properties dataSourceProperties() {
        return new Properties();
    }
}
